package com.example.ymm.modules1.controller.sys;

import com.example.ymm.dto.Constants;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.DbBook;
import com.example.ymm.modules1.service.dto.DbBookQueryCriteria;
import com.example.ymm.modules1.service.user.DbBookService;
import com.example.ymm.modules1.service.user.DbClazzBookService;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/book")
@Api(tags = "pc端 书本管理")
@Slf4j
@CrossOrigin
public class SysBookController extends BaseController {

    @Autowired
    DbBookService dbBookService;
    @Autowired
    DbClazzBookService dbClazzBookService;

    @ApiOperation(value = "列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list(@Valid DbBookQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbBook> list = dbBookService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "所有书本")
    @RequestMapping(value = "/allList", method = RequestMethod.POST)
    public CommonResult allList(@Valid DbBookQueryCriteria criteria) {
        try{
            List<DbBook> list = dbBookService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult save(DbBook book,String clazzIds) {
        try{
            dbBookService.saveOrUpdate(book);
            if(UtilValidate.isNotEmpty(clazzIds)){
                dbClazzBookService.setBookByClazzIds(clazzIds,book.getBookId());
            }
            return CommonResult.success(book);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public CommonResult detail(@ApiParam(value = "数据主键") @RequestParam(required = true) String bookId) {
        try{
            DbBook book = dbBookService.getById(bookId);
            //log.info("---book:"+book.toString());
            return CommonResult.success(book);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "上架")
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public CommonResult up(@ApiParam(value = "数据主键") @RequestParam(required = true) String bookId) {
        try{
            DbBook book = dbBookService.getById(bookId);
            book.setState(Constants.STATE_UP);
            dbBookService.saveOrUpdate(book);
            return CommonResult.success(book);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "下架")
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    public CommonResult down(@ApiParam(value = "数据主键") @RequestParam(required = true) String bookId) {
        try{
            DbBook book = dbBookService.getById(bookId);
            book.setState(Constants.STATE_DOWN);
            dbBookService.saveOrUpdate(book);
            return CommonResult.success(book);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@ApiParam(value = "数据主键") @RequestParam(required = true) String bookId) {
        try{
            dbBookService.removeById(bookId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

}
